package me.firstapp.api.system;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.system.EmailConfig;
import me.firstapp.service.system.EmailConfigService;

@Controller
@RequestMapping(value = "/emailConfig")
@Api(value = "/emailConfig", description = "邮件配置相关接口")
public class EmailConfigApi extends BaseApi {

	@Autowired
	private EmailConfigService emailConfigService;

	@RequestMapping(value = "addEmailConfig.do", method = RequestMethod.POST)
	@ApiOperation(value = "新增邮件配置", notes = "新增邮件配置")
	public void addEmailConfig(@RequestBody Map<String, Object> body, String apiUser, String key,
			HttpServletRequest request, HttpServletResponse response) {
		SingleObject<EmailConfig> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			String apiUrl = (String) body.get("apiUrl");
			String mailFrom = (String) body.get("mailFrom");
			EmailConfig emailConfig = emailConfigService.addEmailConfig(apiClientId, apiUrl, apiUser, key, mailFrom);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(emailConfig);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "updateEmailConfig.do", method = RequestMethod.POST)
	@ApiOperation(value = "更新邮件配置", notes = "更新邮件配置")
	public void updateEmailConfig(@RequestBody Map<String, Object> body, Long id, String apiUser, String key,
			Integer status, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<EmailConfig> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			String apiUrl = (String) body.get("apiUrl");
			String mailFrom = (String) body.get("mailFrom");
			EmailConfig emailConfig = emailConfigService.updateEmailConfig(id, apiClientId, apiUrl, apiUser, key,
					mailFrom, status);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(emailConfig);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findEmailConfigPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询邮件配置", notes = "分页查询邮件配置")
	public void findEmailConfigPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, HttpServletRequest request, HttpServletResponse response) {
		PageObject<EmailConfig> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<EmailConfig> configPage = emailConfigService.findEmailConfigPage(pageNo, pageSize, searchParams,
					sortParam, sortType, apiClientId);
			JsonPage<EmailConfig> jsonPage = new JsonPage<EmailConfig>();
			jsonPage.setList(configPage.getEntities());
			jsonPage.setPageNo(configPage.getPageNo());
			jsonPage.setPageSize(configPage.getPageSize());
			jsonPage.setTotalCount(configPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/deleteEmailConfigs.do", method = RequestMethod.POST)
	@ApiOperation(value = "删除邮件配置", notes = "删除邮件配置")
	public void deleteEmailConfigs(String ids, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			emailConfigService.deleteSections(ids, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/findEmailConfigById.do", method = RequestMethod.GET)
	@ApiOperation(value = "通过ID查询邮件配置", notes = "通过ID查询邮件配置")
	public void findEmailConfigById(Long id, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<EmailConfig> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			EmailConfig emailConfig = emailConfigService.findById(id, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(emailConfig);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}
}
